package org.code.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ExceptFile {
	public static void writeFile(String strAll, String url) {
		FileWriter fw = null;

		OutputStreamWriter pw = null;
		try {
			fw = new FileWriter(url);

			pw = new OutputStreamWriter(new FileOutputStream(url), "utf-8");

			pw.write(strAll);
		} catch (IOException e) {
			e.printStackTrace();
			try {
				if (fw != null) {
					fw.close();
				}

				if (pw != null)
					pw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (fw != null) {
					fw.close();
				}

				if (pw != null)
					pw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String readFile(String url) {
		String strAll = "";
		FileReader fr = null;
		BufferedReader br = null;
		LineNumberReader lnr = null;
		try {
			String strLine = "";

			br = new BufferedReader(new FileReader(url));

			lnr = new LineNumberReader(br);
			
			Set<String> set = new HashSet<String>();
			
			while ((strLine = lnr.readLine()) != null) {
				 if(!strLine.isEmpty()){
					// System.out.println(strLine);
					 set.add(strLine.trim());
				 }
				//strAll = strAll + strLine + "\r\n";
			}
				
			for (String string : set) {
				strAll += "'"+string+"市',";
			}
			System.out.println(strAll);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			try {
				if (fr != null) {
					fr.close();
				}
				if (br != null) {
					br.close();
				}
				if (lnr != null)
					lnr.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
			try {
				if (fr != null) {
					fr.close();
				}
				if (br != null) {
					br.close();
				}
				if (lnr != null)
					lnr.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
				if (br != null) {
					br.close();
				}
				if (lnr != null)
					lnr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return strAll;
	}

	public static void main(String[] args) {
		 readFile("d:/1.txt");
	}
}
